package br.com.mensageiro.negocio.servico.impl;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import br.com.mensageiro.negocio.servico.IEmailService;
import br.com.mensageiro.negocio.servico.email.Email;

@Service("emailService")
public class EmailService implements IEmailService {

	@Autowired
	private MailSender mailSender;
	@Autowired
	private SimpleMailMessage templateMessage;

	@PostConstruct
	public void init() {
		System.out.println("Service iniciado " + this.getClass().getName());
	}

	@Override
	public void enviarEmail(Email email) throws Exception {
		SimpleMailMessage message = new SimpleMailMessage(templateMessage);

		message.setTo(email.getDestinatarios().get(0).getEnderecoEmail());
		message.setSubject(email.getAssunto());
		message.setText(email.getMensagem().getMensagemTexto());
		mailSender.send(message);
	}

}
